//
//  TopBar.m
//  AsyncImages
//
//  Created by Matthew on 11-12-25.
//  Copyright 2011年 develop4u.gr. All rights reserved.
//

#import "TopBar.h"
#import "ColorUtil.h"
#import "IPhone_IPad_Judge.h"

@implementation TopBar

@synthesize suffix;
@synthesize useReturnBtn;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        useReturnBtn = FALSE;
    }
    return self;
}

-(void)    initUI:(NSString *)titleTxt 
   logoDisplay:(BOOL) inLogoDisplay
               type:(int) inType
{
    //init message
    detail_view_exit = [[NSString alloc] initWithFormat:@"detail_view_exit_%@", suffix];
    page_refresh = [[NSString alloc] initWithFormat:@"page_refresh_%@", suffix];
    share_btn_trigger = [[NSString alloc] initWithFormat:@"share_btn_trigger", suffix];
    search_action_trigger = [[NSString alloc] initWithFormat:@"search_action_trigger", suffix];
    
    // Initialization code
    UIColor *color = [ColorUtil stringHexToUIColor:@"0x112033"];//[UIColor redColor];
    
    topBgBar = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.frame.size.width, 45)];
    topBgBar.backgroundColor = color;
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 45)];
    titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Medium" size:18.0];
    [titleLabel setBackgroundColor:[UIColor clearColor]];
    [titleLabel setTextColor:[UIColor whiteColor]];
    titleLabel.textAlignment = UITextAlignmentCenter;

    returnButton = [[ImageUIButton alloc] initWithFrame:CGRectMake(10,25, 55, 30)];
    [returnButton setImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateNormal];
    [returnButton setImage:[UIImage imageNamed:@"return.png"] forState:UIControlStateSelected];
    [returnButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];
    returnButton.button_id = 10;
    
    refreshButton = [[ImageUIButton alloc] initWithFrame:CGRectMake(250, 7.5, 63, 30)];
    if (inType == 2) 
    {
        [refreshButton setImage:[UIImage imageNamed:@"search_btn.png"] forState:UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"search_btn.png"] forState:UIControlStateSelected];
        refreshButton.button_id = 12;
    }
    else
    {
        [refreshButton setImage:[UIImage imageNamed:@"refresh_btn.png"] forState:UIControlStateNormal];
        [refreshButton setImage:[UIImage imageNamed:@"refresh_btn.png"] forState:UIControlStateSelected];
        refreshButton.button_id = 11;
    }
    
    [refreshButton  addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];

    shareButton = [ImageUIButton buttonWithType:UIButtonTypeCustom];
    shareButton.button_id = 2000;
    if ([IPhone_IPad_Judge isIPad]) 
    {
        [shareButton  addButtonWithImageLink:@"share_btn_normal.png" downStatusImgLink:@"share_btn_down.png"];
    }
    else
    {
        [shareButton addButtonWithImageLink:@"share_btn_normal_iphone.png" downStatusImgLink:@"share_btn_down_iphone.png"];
    }
    [shareButton setXY:280 offy:30];
    [shareButton addTarget:self action:@selector(mouseDownProc:) forControlEvents:UIControlEventTouchDown];

    logo = [[UIImageView alloc] initWithFrame:CGRectMake(70, 8, 26, 26)];
    
    [self addSubview:topBgBar];
    [topBgBar addSubview:titleLabel];
    
    if (inType == 0 || inType == 2) 
    {
        [topBgBar addSubview:refreshButton];
        [topBgBar addSubview:logo];
    }
    else if(inType == 3)
    {
        [topBgBar addSubview:logo];
    }
    else if (inType == 1) 
    {
        [self addSubview:returnButton];
        [self addSubview:shareButton];
    }
    
    if (useReturnBtn) 
    {
        [self addSubview:returnButton];
    }
    
    if ([titleTxt length]>11) 
    {
        [titleLabel setText:[titleTxt substringToIndex:11]];
    }
    else
    {
        [titleLabel setText:titleTxt];
    }
    
    if (inLogoDisplay) 
    {
        [logo setImage:[UIImage imageNamed:@"zl_small_logo.png"]];
    }
}

-(void) updateTitle:(NSString *) inTitle
{
    if (titleLabel) 
    {
        if ([inTitle length]>11) 
        {
            [titleLabel setText:[inTitle substringToIndex:11]];
        }
        else
        {
            [titleLabel setText:inTitle];
        }
    }
}

-(void) closeShareBtn
{
    if (shareButton != Nil) 
    {
        shareButton.hidden = TRUE;
    }
}

-(void) mouseDownProc:(id)sender
{
    ImageUIButton *button = sender;
    
    if (button.button_id == 10) //back
    {
        NSLog(@"exit message%@",detail_view_exit);
        
        [[NSNotificationCenter defaultCenter] postNotificationName:detail_view_exit object:self];
    }
    else if(button.button_id == 11)//refresh
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:page_refresh object:self];
    }
    else if(button.button_id == 12)//search
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:search_action_trigger  object:self];
    }
    else if(button.button_id == 2000)//share
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:share_btn_trigger object:self];
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
